namespace( "org.jaxson.util.collections" );

org.jaxson.util.collections.List = function(){

	var obj = {};
	var idx = 0;

	var that ={

		size : function()
		{	
			return idx;
		},
		
		isEmpty : function()
		{	
			return idx === 0;
		},
		
		add : function(value)
		{	
			obj[idx++] = value;
		},
		
		set : function(index, value)
		{	
			if( index < 0 || index > idx ){
				throw "IllegalArgumentException: index must be greater than zero and less than size";
			}
			
			if( obj[index] !== undefined ){
				
				var nidx = 0;
				var nobj = {};
				for( i in obj ){
					
					if( parseInt(i) === index ){
						nobj[index] = value;
						nidx++;
					}
					
					nobj[nidx++] = obj[i];					
				}
				
				obj = nobj;
				idx = nidx;
			}
		},
		
		get : function(index)
		{	
			return obj[index];
		},
		
		first : function()
		{	
			return this.isEmpty()?null:get(0);
		},
		
		last : function()
		{	
			return this.isEmpty()?null:get(idx);
		},
		
		remove : function(index)
		{	
			if( obj[index] !== undefined ){
				
				var nidx = 0;
				var nobj = {};
				for( i in obj ){
					
					if( i === index ){
						continue;
					}
					
					nobj[nidx++] = obj[i];					
				}
				
				obj = nobj;
				idx = nidx;
			}
		},
		
		contains : function(value)
		{	
			return (this.indexOf(value) != -1);
		},
		
		indexOf : function(value)
		{	
			if(value)
			{	
				for( i in obj ){
					if( obj[i] === value ){
						return i;
					}
				}
			}
			
			return -1;
		},
		
		lastIndexOf : function(value)
		{	
			if(value)
			{	
				for( i=idx; i>=0; i-- ){
					if( obj[i] === value ){
						return i;
					}
				}
			}
		
			return -1;
		},
		
		clear : function()
		{	
			idx = 0;
			
			var arr = this.asArray;
			delete obj;
			obj = {};
			
			for( i in arr ){
				delete arr[i];
			}
		},
		
		asArray : function(){
			var result = new Array();
			
			for( i in obj ){
				result.push( obj[i] );
			}
			
			return result;
		}
		/*,
		
		toString : function()
		{	
			return "[" + ns + " List]";
		}
		*/
	};
	
	inherits ("JaxsonObject", "org.jaxson.util.collections.List", that);
	return that;
};